//
//  GTWelcomeController.swift
//  swiftWEIbo
//
//  Created by mac－gt on 15/11/25.
//  Copyright © 2015年 mac－gt. All rights reserved.
//

import UIKit

class GTWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUp()
        if GTAcounts.loadAccount()?.avatar_large != nil{
            iconView.image=UIImage(named: (GTAcounts.loadAccount()?.avatar_large)!)
        }
        
    }
    //在页面显示完之后调用动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnima()  
    }
  private lazy var iconViewContant=NSLayoutConstraint()
    
    //添加子控件和约束
    
    func setUp(){
          view.addSubview(bgView)
        view.addSubview(iconView)
        view.addSubview(titleLable)
        iconView.translatesAutoresizingMaskIntoConstraints=false
        bgView.translatesAutoresizingMaskIntoConstraints=false
        titleLable.translatesAutoresizingMaskIntoConstraints=false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bvi]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bvi":bgView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv":bgView]))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
          view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 160))
        iconViewContant=view.constraints.last!
        view.addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: titleLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    }
    //头像的动画
    func startAnima(){
        
        iconViewContant.constant = 500
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        self.view.layoutIfNeeded()
        }) { (_) -> Void in
            (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
        }
    }
    //懒加载头像View
    private lazy var iconView:UIImageView={
          let image=UIImage(named: "avatar_default_big")
        let imgView=UIImageView(image: image)
        imgView.layer.cornerRadius=42.5
        imgView.layer.masksToBounds=true
        return imgView
    }()
    //懒加载界面背景
    private lazy var bgView:UIImageView={
        let imgView=UIImageView(image: UIImage(named: "ad_background"))
        return imgView
    }()
    //懒加载欢迎Label
    private lazy var titleLable:UILabel={
        let lab=UILabel()
        lab.text="欢迎归来"
        lab.sizeToFit()
        return lab
    }()
}
